<?php

class RequestedElementManager extends TModule
{
	// -------------------- basic functions --------------------
	public function addRequestedElement($name, $status, $description = null,
										$session_id = null, $owner_id = null)
	{
		$element = new RequestedElementRecord();
		$element->setData(0, $name, $status, $description, $session_id, $owner_id);
		
		if($element->save())
			return $element;
		else
			return null;
	}
	
	public function deleteRequestedElement($id)
	{
		return RequestedElementRecord::finder()->deleteById($id);
	}
	
	public function updateRequestedElement($id, $name, $status, $description, $session_id, $owner_id)
	{	
		$elementRecord = new RequestedElementRecord();
		$elementRecord->setData($id, $name, $status, $description, $session_id, $owner_id);
		return $this->updateRequestedElementByRequestedElementRecord($elementRecord);
	}

	public function updateRequestedElementByRequestedElementRecord($elementRecord)
	{	
		if($elementRecord instanceof RequestedElementRecord)
		{
			$finder = RequestedElementRecord::finder();
			$id = $elementRecord->getId();
			if($id > 0 && $rec = $finder->findByPk($id))
			{
				if($rec->equals($elementRecord))
					return $rec;
				$rec->setDataByRequestedElementRecord($elementRecord);
				if($rec->save())
					return $rec;
			}
		}
		return null;
	}
	
	public function getRequestedElementById($id)
	{
		return RequestedElementRecord::finder()->findById($id);
	}
	
	// -------------------- extended functions --------------------
	public function getRequestedElementsList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countRequestedElements();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return RequestedElementRecord::finder()->findAll($criteria);
	}
	
	public function getRequestedElementsListByOwnerId($owner_id, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'owner_id = :owner_id';
		$criteria->Parameters[':owner_id'] = $owner_id;
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countRequestedElementsByOwnerId($owner_id);
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return RequestedElementRecord::finder()->findAll($criteria);
	}
	
	public function getRequestedElementsListBySessionId($session_id, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'session_id = :session_id';
		$criteria->Parameters[':session_id'] = $session_id;
		
		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countRequestedElementsBySessionId($session_id);
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return RequestedElementRecord::finder()->findAll($criteria);
	}
	
	public function getRequestedElementsListByStatus($status = null, $revert = false, $offset = -1, $limit = -1)
	{
		if($status === null)
		{
			return $this->getRequestedElementsList($revert, $offset, $limit);
		}
		else
		{
			$criteria = new TActiveRecordCriteria();
			$criteria->Condition = 'status = :status';
			$criteria->Parameters[':status'] = $status;
		
			if($revert)
				;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
			if($offset < 0 || $offset == null)
				$offset = 0;
			if($limit < 0 || $limit == null)
				$limit = $this->countRequestedElementsByStatus($status);
				
			$criteria->Offset = $offset;
			$criteria->Limit = $limit;
		
			return RequestedElementRecord::finder()->findAll($criteria);
		}
	}
	
	public function getRequestedElementsListByOwnerIdStatus($owner_id, $status = null, $revert = false, $offset = -1, $limit = -1)
	{
		if($status === null)
		{
			return $this->getRequestedElementsListByOwnerId($owner_id, $revert, $offset, $limit);
		}
		else
		{
			$criteria = new TActiveRecordCriteria();
			$criteria->Condition = 'status = :status AND owner_id = :owner_id';
			$criteria->Parameters[':status'] = $status;
			$criteria->Parameters[':owner_id'] = $owner_id;
		
			if($revert)
				;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
			if($offset < 0 || $offset == null)
				$offset = 0;
			if($limit < 0 || $limit == null)
				$limit = $this->countRequestedElementsByOwnerIdStatus($owner_id, $status);
				
			$criteria->Offset = $offset;
			$criteria->Limit = $limit;
		
			return RequestedElementRecord::finder()->findAll($criteria);
		}
	}
	
	public function getRequestedElementsListBySessionIdStatus($session_id = null, $status = null, $revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		if($status === null && $session_id === null)
		{
		}
		elseif($status === null && $session_id !== null)
		{
			$criteria->Condition = 'session_id = :session_id';
			$criteria->Parameters[':session_id'] = $session_id;
		}
		elseif($status !== null && $session_id === null)
		{
			$criteria->Condition = 'status = :status';
			$criteria->Parameters[':status'] = $status;
		}
		else
		{	
			$criteria->Condition = 'status = :status AND session_id = :session_id';
			$criteria->Parameters[':status'] = $status;
			$criteria->Parameters[':session_id'] = $session_id;
		}

		if($revert)
			;//$criteria->OrdersBy['start_datetime'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countRequestedElementsBySessionIdStatus($session_id, $status);
				
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return RequestedElementRecord::finder()->findAll($criteria);
	}
	
	public function countRequestedElements()
	{
		return RequestedElementRecord::finder()->count();
	}
	
	public function countRequestedElementsByOwnerId($owner_id)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'owner_id = :owner_id';
		$criteria->Parameters[':owner_id'] = $owner_id;
		
		return RequestedElementRecord::finder()->count($criteria);
	}
	
	public function countRequestedElementsBySessionId($session_id)
	{
		$criteria = new TActiveRecordCriteria();
		$criteria->Condition = 'session_id = :session_id';
		$criteria->Parameters[':session_id'] = $session_id;
		return RequestedElementRecord::finder()->count($criteria);
	}
	
	public function countRequestedElementsByStatus($status = null)
	{
		if($status === null)
			return RequestedElementRecord::finder()->count();
		else
		{
			$criteria = new TActiveRecordCriteria();
			$criteria->Condition = 'status = :status';
			$criteria->Parameters[':status'] = $status;
			
			return RequestedElementRecord::finder()->count($criteria);
		}
	}
	
	public function countRequestedElementsByOwnerIdStatus($owner_id, $status = null)
	{
		if($status === null)
		{
			return $this->countRequestedElementsByOwnerId($owner_id);
		}
		else
		{
			$criteria = new TActiveRecordCriteria();
			$criteria->Condition = 'status = :status AND owner_id = :owner_id';
			$criteria->Parameters[':status'] = $status;
			$criteria->Parameters[':owner_id'] = $owner_id;
			
			return RequestedElementRecord::finder()->count($criteria);
		}
	}
	
	public function countRequestedElementsBySessionIdStatus($session_id = null, $status = null)
	{
		if($status === null && $session_id === null)
		{
			return $this->countRequestedElements();
		}
		elseif($status === null && $session_id !== null)
		{
			return $this->countRequestedElementsBySessionId($session_id);
		}
		elseif($status !== null && $session_id === null)
		{
			return $this->countRequestedElementsByStatus($status);
		}
		else
		{
			$criteria = new TActiveRecordCriteria();
			$criteria->Condition = 'status = :status AND session_id = :session_id';
			$criteria->Parameters[':status'] = $status;
			$criteria->Parameters[':session_id'] = $session_id;
			
			return RequestedElementRecord::finder()->count($criteria);
		}
	}
}

?>